#!/usr/bin/env python
# encoding: utf-8
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer('embedding_dim', 300, 'dimension of word embedding')
tf.app.flags.DEFINE_integer('position_embedding_dim', 200, 'dimension of word embedding')
tf.app.flags.DEFINE_integer('batch_size', 25, 'number of example per batch')
tf.app.flags.DEFINE_integer('n_hidden', 300, 'number of hidden unit')
tf.app.flags.DEFINE_float('learning_rate_1', 0.001, 'learning rate')
tf.app.flags.DEFINE_float('learning_rate_2', 0.1, 'learning rate')
tf.app.flags.DEFINE_float('dev_sample_percentage', 0.2, 'dev rate')
tf.app.flags.DEFINE_integer('n_class', 3, 'number of distinct class')
tf.app.flags.DEFINE_integer('max_sentence_len', 80, 'max number of tokens per sentence')
tf.app.flags.DEFINE_integer('max_target_len', 10, 'max target length')
tf.app.flags.DEFINE_float('l2_reg', 0.00001, 'l2 regularization')
tf.app.flags.DEFINE_float('random_base', 0.1, 'initial random base')
tf.app.flags.DEFINE_integer('display_step', 4, 'number of test display step')
tf.app.flags.DEFINE_integer('n_iter', 30, 'number of train iter')
tf.app.flags.DEFINE_float('keep_prob1', 0.5, 'dropout keep prob')
tf.app.flags.DEFINE_float('keep_prob2', 0.5, 'dropout keep prob')
tf.app.flags.DEFINE_string('t1', 'last', 'type of hidden output')
tf.app.flags.DEFINE_string('t2', 'last', 'type of hidden output')
tf.app.flags.DEFINE_integer('n_layer', 3, 'number of stacked rnn')

tf.app.flags.DEFINE_string('pre_trained', 'sentence_tranfer', 'pre_trained model')

tf.app.flags.DEFINE_string('train_file_path_1', 'data/twitter/twitter_train_4.txt', 'training file')
tf.app.flags.DEFINE_string('train_file_path_2', 'data/twitter/twitter_train_4.txt', 'training file')
tf.app.flags.DEFINE_string('validate_file_path_1', 'data/twitter/twitter_dev_4.txt', 'validating file')
tf.app.flags.DEFINE_string('validate_file_path_2', 'data/twitter/twitter_dev_4.txt', 'validating file')

tf.app.flags.DEFINE_string('embedding_file_path', 'data/twitter/twitter_2014_840b_300.txt', 'embedding file')

tf.app.flags.DEFINE_string('saver_checkpoint_1', 'data/twitter/pp_no_2L_checkpoint4', 'prob')
tf.app.flags.DEFINE_string('saver_checkpoint_2', 'data/twitter/pp_no_2L_checkpoint4', 'prob')

tf.app.flags.DEFINE_string('prob_file', 'prob1.txt', 'prob')


def print_config():
    FLAGS.flag_values_dict()
    print('\nParameters:')
    for k, v in sorted(FLAGS.__flags.items()):
        print('{}={}'.format(k, v))


def loss_func(y, prob):
    reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prob, labels=y)) + sum(reg_loss)
    loss = - tf.reduce_mean(y * tf.log(prob)) + sum(reg_loss)
    return loss


def acc_func(y, prob):
    correct_pred = tf.equal(tf.argmax(prob, 1), tf.argmax(y, 1))
    acc_num = tf.reduce_sum(tf.cast(correct_pred, tf.int32), name='acc_number')
    acc_prob = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    return acc_num, acc_prob